local UI = SecretaryUI

local INSET_FRAME_BORDER = 2
local SLIDER_SIZE = 20
local BASE_FRAME_SPACE = 200

local BaseFrame_OnMouseDown = function (self)
	self:StartMoving()
end

local BaseFrame_OnMouseUp = function (self)
	self:StopMovingOrSizing()
	self.object:OnFrameMoved()
end

local function Layout(frame)
	
	frame.close:ClearAllPoints()
	frame.close:SetPoint("TOPRIGHT", -INSET_FRAME_BORDER, -INSET_FRAME_BORDER)
	
	frame.title:ClearAllPoints()
	frame.title:SetPoint("TOPLEFT", INSET_FRAME_BORDER, -INSET_FRAME_BORDER)
	frame.title:SetPoint("BOTTOMRIGHT", frame.close, "BOTTOMLEFT", -INSET_FRAME_BORDER, 0)
	
	frame.vslider:ClearAllPoints()
	frame.vslider:SetPoint("TOPRIGHT", frame.close, "BOTTOMRIGHT", 0, -INSET_FRAME_BORDER)
	frame.vslider:SetPoint("BOTTOMLEFT", INSET_FRAME_BORDER + SLIDER_SIZE, BASE_FRAME_SPACE)
	
	frame.hslider:ClearAllPoints()
	frame.hslider:SetPoint("TOPLEFT", INSET_FRAME_BORDER, BASE_FRAME_SPACE + SLIDER_SIZE)
	frame.hslider:SetPoint("BOTTOMRIGHT", INSET_FRAME_BORDER + SLIDER_SIZE, BASE_FRAME_SPACE)
	
	frame.container:ClearAllPoints()
	frame.container:SetPoint("TOPLEFT", frame.title, "BOTTOMLEFT")
	frame.container:SetPoint("BOTTOMRIGHT", frame.vslider, "BOTTOMLEFT")

end

local function CreateBaseFrame(object)
	local frame = CreateFrame("Frame", "TempFrame", UIParent)
	
	frame:SetToplevel(true)
	frame:SetMovable(true)
	frame:EnableMouse(true)
	
	frame:SetScript("OnMouseDown", BaseFrame_OnMouseDown)
	frame:SetScript("OnMouseUp", BaseFrame_OnMouseUp)
	
	frame:SetWidth(object.vars.width)
	frame:SetHeight(object.vars.height)
	frame:SetPoint("BOTTOMLEFT", object.vars.left, object.vars.bottom)
	
	frame.hslider = UI.CreateSlider(frame, false)
	frame.vslider = UI.CreateSlider(frame, true)
	frame.container = CreateFrame("ScrollFrame", nil, frame)

	frame.close = UI.CreateCloseButton(frame)
	
	for type in pairs(UI.ResizeBorderTypes) do
		frame["resize_"..type] = UI.CreateResizeButton(frame, type)
	end
	
	frame.title = UI.CreateTitle(frame)
	frame.title.text:SetText(object.title)
	
	object.frame = frame
	frame.object = object
	
	frame.container = UI.CreateContainerFrame(frame)
	
	frame:SetBackdrop({
		bgFile = "Interface\\TutorialFrame\\TutorialFrameBackground",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true,
		edgeSize = 16,
		tileSize = 32,
		insets = {
			left = 5,
			right = 5,
			top = 5,
			bottom = 5
		}
	})
	
	Layout(frame)
	
	return frame
end

UI.CreateFrame = CreateBaseFrame
